package pers.vic.practice.module.personal.controller;

import com.fasterxml.jackson.databind.ser.VirtualBeanPropertyWriter;
import oracle.jdbc.proxy.annotation.Post;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import pers.vic.boot.base.model.BaseResponse;
import pers.vic.boot.base.model.PageInfo;
import pers.vic.boot.base.vo.Select2VO;
import pers.vic.practice.module.student.model.SClasses;
import pers.vic.practice.module.student.model.SCourse;
import pers.vic.practice.module.student.model.SCourseSelection;
import pers.vic.practice.module.student.model.SStudent;
import pers.vic.practice.module.student.service.SClassesService;
import pers.vic.practice.module.student.service.SCourseSelectionService;
import pers.vic.practice.module.student.service.SCourseService;
import pers.vic.practice.module.student.service.SStudentService;
import pers.vic.practice.util.SystemSessionHelper;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 描述:学生维护个人信息
 * @author Vic.xu
 * @date 2022-04-22 14:04
 */
@RestController
@RequestMapping("/personal")
public class PersonalController {


    @Resource
    private SStudentService studentService;

    @Resource
    private SCourseService courseService;

    @Resource
    private SCourseSelectionService sCourseSelectionService;

    @Resource
    private SClassesService classesService;
    @GetMapping("/home")
    public ModelAndView home(){
        String number = SystemSessionHelper.getNumber();
        ModelAndView view = new ModelAndView("personal/home");
        SStudent entity = studentService.findByNumber(number);
        view.addObject("entity", entity);
        bindInitSelect(view);
        return view;
    }


    @GetMapping("/score")
    public ModelAndView score(){
        ModelAndView view = new ModelAndView("personal/score");
        bindInitSelect(view);
        return view;
    }

    @GetMapping("/scoreList")
    public BaseResponse<?> scoreList(SCourseSelection lookup){
        String number = SystemSessionHelper.getNumber();
        lookup.setNumber(number);
        PageInfo<SCourseSelection> page = sCourseSelectionService.page(lookup);
        return BaseResponse.success(page);
    }


    @PostMapping("/save")
    public BaseResponse save(SStudent student){
        studentService.saveInfo(student);
        return BaseResponse.success(student);
    }

    private void bindInitSelect(ModelAndView view){
        view.addObject("courses", courseService.list(new SCourse()).stream().map(s->{
            return new Select2VO(s.getId(), s.getName());
        }).collect(Collectors.toList()));
        List<Select2VO> classes = classesService.list(new SClasses()).stream().map(s -> new Select2VO(s.getId(), s.getName())).collect(Collectors.toList());
        view.addObject("classes", classes);
    }
}
